import LanguageMngTs from "./LanguageMngTs";
import GameDataUtilTs from "../Utils/GameDataUtilTs";
import TestConfig from "../Static/TestConfig";
import KYCEnum from "../Static/KYCEnum";

cc.Class({
    extends: cc.Component,

    properties: {
        labelCashLocal: {
            default: null,
            type: cc.Label,
        },

        labelCashGlobal: {
            default: null,
            type: cc.Label,
        },

        labelCashTotal: {
            default: null,
            type: cc.Label,
        },

        labelBuy: {
            default: null,
            type: cc.Label,
        },

        spriteBtnBuy: {
            default: null,
            type: cc.Sprite
        },

        labelContentTitle: {
            default: null,
            type: cc.Label,
        },

        labelAddContentTitle: {
            default: null,
            type: cc.Label,
        },

        levelParentBuy: {
            default: null,
            type: cc.Node
        },

        levelParentWithDraw: {
            default: null,
            type: cc.Node
        },

        frameBetBlue: {
            default: null,
            type: cc.SpriteFrame
        },

        frameBetRed: {
            default: null,
            type: cc.SpriteFrame
        },

        frameBtnYellow: {
            default: null,
            type: cc.SpriteFrame
        },

        frameBtnGreen: {
            default: null,
            type: cc.SpriteFrame
        },

        frameBtnGray: {
            default: null,
            type: cc.SpriteFrame
        },

        btnBuySprite: {
            default: null,
            type: cc.Sprite
        },

        btnWithDrawSprite: {
            default: null,
            type: cc.Sprite
        },

        coinIcon1: {
            default: null,
            type: cc.Node,
        },

        coinIcon2: {
            default: null,
            type: cc.Node,
        },

        coinIcon3: {
            default: null,
            type: cc.Node,
        },

        _acceptClicked: false,

        lang_label1: {
            default: null,
            type: cc.Label
        },

        lang_label2: {
            default: null,
            type: cc.Label
        },

        lang_label3: {
            default: null,
            type: cc.Label
        },

        lang_label4: {
            default: null,
            type: cc.Label
        },

        lang_label5: {
            default: null,
            type: cc.Label
        },

        lang_label6: {
            default: null,
            type: cc.Label
        },

        lang_label7: {
            default: null,
            type: cc.Label
        },

        lang_label8: {
            default: null,
            type: cc.Label
        },

        lang_label9: {
            default: null,
            type: cc.Label
        },

        lang_label10: {
            default: null,
            type: cc.Label
        },

        lang_label17: {
            default: null,
            type: cc.Label
        },

        withdrawLimit: {
            default: null,
            type: cc.Label
        },

        wallet_Tips1: {
            default: null,
            type: cc.Node
        },

        wallet_Tips2: {
            default: null,
            type: cc.Node
        },

        wallet_Tips3: {
            default: null,
            type: cc.Node
        },
    },

    getBuyItem(pack_id) {
        for (let i = 0; i < cc.gg.gameDataUtil.shopBuy.length; i++) {
            let item = cc.gg.gameDataUtil.shopBuy[i]
            if (item.pack_id === pack_id)
                return item
        }
        return null
    },

    onLoad() {
        this._acceptClicked = true
        let info = cc.gg.dataMgr.getPlayerInfo()

        this.labelCashLocal.string = cc.gg.gameDataUtil.getStringOfRupee(info.deposit)
        this.labelCashGlobal.string = cc.gg.gameDataUtil.getStringOfRupee(info.winnings)
        this.labelCashTotal.string = cc.gg.gameDataUtil.getStringOfRupee(info.deposit + info.winnings)
        this.labelContentTitle.string = cc.gg.gameDataUtil.getStringOfRupee(info.deposit + info.winnings)
        this.labelAddContentTitle.string = cc.gg.gameDataUtil.getStringOfRupee(info.deposit + info.winnings)
        this.withdrawLimit.string = info.withdraw_limit + " times left";

        this.coinIcon1.active = !cc.gg.gameDataUtil.showRupee
        this.coinIcon2.active = !cc.gg.gameDataUtil.showRupee
        this.coinIcon3.active = !cc.gg.gameDataUtil.showRupee

        this.selectBuyLevel(0)
        this._showWithDraw();
        this.language()
        this.node.getChildByName("withDraw").active = false;
        this.node.getChildByName("balance").active = true;
        this.node.getChildByName("deposit").active = false;

        this.node.on(cc.Node.EventType.TOUCH_START, () => {
            this.wallet_Tips1.active = false;
            this.wallet_Tips2.active = false;
            this.wallet_Tips3.active = false;
        })
    },

    _initLevels: function (staticData) {
        for (let i = 0; i < staticData.length; i++) {
            let obj = staticData[i]
            let levelNode = this.getLevelParent().children[i]
            let coinParent = cc.find("value/coin", levelNode)
            let coinLabel = cc.find("label", coinParent)
            if (cc.gg.gameDataUtil.showRupee) {
                coinParent.active = false
            } else {
                coinParent.active = true
                coinLabel.getComponent(cc.Label).string = obj.coin
            }
            cc.find("value/cash/label", levelNode).getComponent(cc.Label).string = obj.price
        }
    },

    language() {
        this.lang_label1.string = LanguageMngTs.getSentence(101);  //Cash Account
        this.lang_label2.string = LanguageMngTs.getSentence(77);  //Deposited
        this.lang_label3.string = LanguageMngTs.getSentence(348);  //winnings
        this.lang_label4.string = LanguageMngTs.getSentence(75);  //Total Balance
        this.lang_label5.string = LanguageMngTs.getSentence(260);  //Deposit
        this.lang_label6.string = LanguageMngTs.getSentence(78);  //WithDraw
        this.lang_label7.string = LanguageMngTs.getSentence(3);  //notice
        this.lang_label8.string = LanguageMngTs.getSentence(261);  //Fill KYC documents
        this.lang_label9.string = LanguageMngTs.getSentence(262);  //Query Transaction Details
        this.lang_label10.string = LanguageMngTs.getSentence(78);  //WithDraw
        this.lang_label17.string = LanguageMngTs.getSentence(270);  //buy
    },

    _showBuy: function () {
        cc.gg.tdUtil.doSendTdEvent("click_show_buy", "Wallet")

        cc.find("buy", this.node.getChildByName("deposit")).active = true

        this.initBuyContentToBuy()
    },

    _showWithDraw: function () {
        cc.gg.tdUtil.doSendTdEvent("click_show_withdraw", "Wallet")
        cc.find("buy", this.node.getChildByName("deposit")).active = true

        this.initBuyContentToWithDraw()
    },

    _showPlzReadNotice: function () {
        let title = LanguageMngTs.getSentence(3) //Notice
        let btnStr = LanguageMngTs.getSentence(181)   //ok
        let contentStr = LanguageMngTs.getSentence(227)   //Please read the notice and click "Accept" first.
        let cb = function () {
            cc.gg.toastMgr.removeDialog()
        }.bind(this)
        cc.gg.toastMgr.showDialogOneButton(title, btnStr, contentStr, cb)
    },

    _showCantKYCSinceCoin: function () {
        let title = LanguageMngTs.getSentence(3) //Notice
        let btnStr = LanguageMngTs.getSentence(181)   //ok
        let contentStr = LanguageMngTs.getSentence(228)   //Your cash is not enough to withdraw,no need to KYC.
        let cb = function () {
            cc.gg.toastMgr.removeDialog()
        }.bind(this)
        cc.gg.toastMgr.showDialogOneButton(title, btnStr, contentStr, cb)
    },

    _initBuyWithDrawButtons: function (read) {
        if (read) {
            // this.btnBuySprite.spriteFrame = this.frameBtnYellow
            // this.btnWithDrawSprite.spriteFrame = this.frameBtnGreen
        } else {
            // this.btnBuySprite.spriteFrame = this.frameBtnGray
            // this.btnWithDrawSprite.spriteFrame = this.frameBtnGray
        }
    },

    onWalletButtonClicked: function (event) {
        let btnName = event.target.name
        if (btnName === "btn_i_know") {
            cc.gg.gameDataUtil.setReadWalletNotice(cc.gg.dataMgr.getPlayerInfo().uid)
            this._acceptClicked = true
            this._showBuy()
            this._initBuyWithDrawButtons(true)
        } else if (btnName === "btn_open_buy") {
            // if(!this._acceptClicked){
            //     this._showPlzReadNotice()
            // }else {
            //     this._showBuy()
            // }
            this._showBuy()
            this.node.getChildByName("withDraw").active = false;
            this.node.getChildByName("balance").active = false;
            this.node.getChildByName("deposit").active = true;
        } else if (btnName === "btn_open_withdraw") {
            // if(!this._acceptClicked){
            //     this._showPlzReadNotice()
            // }else {
            //     this._showWithDraw()
            // }
            this._showWithDraw()
            this.node.getChildByName("withDraw").active = true;
            this.node.getChildByName("balance").active = false;
            this.node.getChildByName("deposit").active = false;
        } else if (btnName === "btn_buy") {
            if (this.walletState === "withdraw") {
                let info = cc.gg.dataMgr.getPlayerInfo()
                if (info.withdraw_limit <= 0) {
                    cc.gg.toastMgr.showToast("No more times today");
                } else {
                    this.withDrawItem(this.selectedLevel)
                }
            } else if (this.walletState === "buy") {
                this.buyItem(this.selectedLevel)
            }
        } else if (btnName === "btn_close") {
            this.closeWallet()
        } else if (btnName === "btn_close1") {
            this.node.getChildByName("withDraw").active = false;
            this.node.getChildByName("balance").active = true;
            this.node.getChildByName("deposit").active = false;
        } else if (btnName === "btn_close2") {
            this.node.getChildByName("withDraw").active = false;
            this.node.getChildByName("balance").active = true;
            this.node.getChildByName("deposit").active = false;
        }
    },

    closeWallet: function () {
        // this.node.destroy()
        cc.gg.gameDataUtil.goToLobby(true);
    },

    initBuyContentToBuy: function () {

        cc.find("buy", this.node.getChildByName("deposit")).active = true
        cc.find("withdraw", this.node.getChildByName("withDraw")).active = true
        this.labelBuy.string = "Buy"
        this.walletState = "buy"
        let levelNodes = this.getLevelParent().children
        for (let i = 0; i < levelNodes.length; i++) {
            let node = levelNodes[i]
            if (node.name === "level1") {
                cc.find("border", node).active = true
            } else {
                cc.find("border", node).active = false
            }
        }
        // this.changeAllItemCashFrame(this.frameBetBlue)
        // this.spriteBtnBuy.spriteFrame = this.frameBtnYellow
        this._initLevels(cc.gg.gameDataUtil.shopBuy)

        let info = cc.gg.dataMgr.getPlayerInfo()
        this.labelContentTitle.string = cc.gg.gameDataUtil.getStringOfRupee(info.deposit)
        this.labelAddContentTitle.string = cc.gg.gameDataUtil.getStringOfRupee(info.deposit)
        this.selectBuyLevel(0)
    },

    initBuyContentToWithDraw: function () {

        cc.find("withdraw", this.node.getChildByName("withDraw")).active = true
        cc.find("buy", this.node.getChildByName("deposit")).active = false
        this.labelBuy.string = "With Draw"
        this.walletState = "withdraw"

        let levelNodes = this.getLevelParent().children
        for (let i = 0; i < levelNodes.length; i++) {
            let node = levelNodes[i]
            if (node.name === "level1") {
                cc.find("border", node).active = true
            } else {
                cc.find("border", node).active = false
            }
        }
        // this.changeAllItemCashFrame(this.frameBetRed)
        // this.spriteBtnBuy.spriteFrame = this.frameBtnGreen
        this._initLevels(cc.gg.gameDataUtil.shopWithDraw)
        let info = cc.gg.dataMgr.getPlayerInfo()
        this.labelContentTitle.string = cc.gg.gameDataUtil.getStringOfRupee(info.winnings)
        this.labelAddContentTitle.string = cc.gg.gameDataUtil.getStringOfRupee(info.winnings)
        this.selectBuyLevel(0)
    },

    initWallet: function (lobbyUIMgr) {
        this.lobbyUIMgr = lobbyUIMgr
        let readNotice = cc.gg.gameDataUtil.getReadWalletNotice(cc.gg.dataMgr.getPlayerInfo().uid)
        if (readNotice) {
            this._acceptClicked = true
            this._showBuy()
        } else {
            this._acceptClicked = false
            cc.find("buy", this.node.getChildByName("deposit")).active = false
            cc.find("withdraw", this.node.getChildByName("withDraw")).active = true

            let info = cc.gg.dataMgr.getPlayerInfo()
            this.labelContentTitle.string = cc.gg.gameDataUtil.getStringOfRupee(info.deposit + info.winnings)
            this.labelAddContentTitle.string = cc.gg.gameDataUtil.getStringOfRupee(info.deposit + info.winnings)
        }
        this._initBuyWithDrawButtons(readNotice)

    },

    getLevelParent: function () {
        let levelParent = this.levelParentBuy
        if (this.walletState === "buy") {
            levelParent = this.levelParentBuy
        } else if (this.walletState === "withdraw") {
            levelParent = this.levelParentWithDraw
        }
        return levelParent
    },

    selectBuyLevel: function (number) {
        let nodeName = null

        let levelParent = this.levelParentBuy
        if (this.walletState === "buy") {
            number = number + 1
            levelParent = this.levelParentBuy
            nodeName = "level" + number
        } else if (this.walletState === "withdraw") {
            levelParent = this.levelParentWithDraw
            nodeName = "level" + (number + 1)
        }
        this.selectedLevel = number

        let levelNodes = levelParent.children
        for (let i = 0; i < levelNodes.length; i++) {
            let node = levelNodes[i]
            if (node.name === nodeName) {
                cc.find("border", node).active = true
                cc.find("value/cash/label", node).color = cc.Color.WHITE;
            } else {
                cc.find("value/cash/label", node).color = cc.Color.BLUE;
                cc.find("border", node).active = false
            }
        }
    },

    changeAllItemCashFrame: function (newFrame) {
        let levelNodes = this.getLevelParent().children
        for (let i = 0; i < levelNodes.length; i++) {
            let node = levelNodes[i]
            cc.find("value/coin", node).getComponent(cc.Sprite).spriteFrame = newFrame
        }
    },

    onLevelButtonClicked: function (event) {
        let btnName = event.target.name
        if (btnName === "level1") {
            this.selectBuyLevel(0)
        } else if (btnName === "level2") {
            this.selectBuyLevel(1)
        } else if (btnName === "level3") {
            this.selectBuyLevel(2)
        } else if (btnName === "level4") {
            this.selectBuyLevel(3)
        } else if (btnName === "level5") {
            this.selectBuyLevel(4)
        } else if (btnName === "level6") {
            this.selectBuyLevel(5)
        } else if (btnName === "level7") {
            this.selectBuyLevel(6)
        } else if (btnName === "level8") {
            this.selectBuyLevel(7)
        } else {
            cc.error("选中了未知的item：", btnName)
        }
    },

    onBtnTipsIsShow: function (event) {
        let btnName = event.target.name
        if (btnName === "wallet_Btn_Tips1") {
            if (this.wallet_Tips1.active == true) {
                this.wallet_Tips1.active = false;
                this.wallet_Tips2.active = false;
                this.wallet_Tips3.active = false;
            } else {
                this.wallet_Tips1.active = true;
                this.wallet_Tips2.active = false;
                this.wallet_Tips3.active = false;
            }
        } else if (btnName === "wallet_Btn_Tips2") {
            if (this.wallet_Tips2.active == true) {
                this.wallet_Tips1.active = false;
                this.wallet_Tips2.active = false;
                this.wallet_Tips3.active = false;
            } else {
                this.wallet_Tips1.active = false;
                this.wallet_Tips2.active = true;
                this.wallet_Tips3.active = false;
            }
        } else if (btnName === "wallet_Btn_Tips3") {
            if (this.wallet_Tips3.active == true) {
                this.wallet_Tips1.active = false;
                this.wallet_Tips2.active = false;
                this.wallet_Tips3.active = false;
            } else {
                this.wallet_Tips1.active = false;
                this.wallet_Tips2.active = false;
                this.wallet_Tips3.active = true;
            }
        }
    },

    onKYCButtonClicked: function (event) {
        this._acceptClicked = true
        if (!this._acceptClicked) {
            this._showPlzReadNotice()
        } else {
            cc.find("Canvas").emit("msgwatcher_add", function () {
                let info = cc.gg.dataMgr.getPlayerInfo()
                if (info) {
                    if (cc.gg.gameDataUtil.openKYCWithoutMoney || info.deposit + info.winnings >= cc.gg.gameDataUtil.shopWithDraw[0].coin) {
                        this.lobbyUIMgr.showKYCScene()
                    } else {
                        this._showCantKYCSinceCoin()
                    }
                    return true
                }
                return false
            }.bind(this))

        }
    },

    onTransactionButtonClicked: function () {
        if (GameDataUtilTs.LoginMethod.GUEST === GameDataUtilTs.getLoginType()) {
            let a = LanguageMngTs.getSentence(3)    //Notice
            let b = LanguageMngTs.getSentence(57)   //Yes
            let c = LanguageMngTs.getSentence(179)  //Cancel
            let d = LanguageMngTs.getSentence(230)  //You must register
            cc.gg.toastMgr.showDialog(a, b, c, d,
                function () {
                    cc.gg.gameDataUtil.goToLogin(true)
                }.bind(this),
                function () {
                    cc.gg.toastMgr.removeDialog()
                }.bind(this)
            )

        } else {
            let mobile = cc.gg.dataMgr.getPlayerInfo().mobile_no;
            if (!mobile) {
                let title = LanguageMngTs.getSentence(3)
                let positiveStr = LanguageMngTs.getSentence(57)   //Yes
                let passiveStr = LanguageMngTs.getSentence(179)   //Cancel
                let content = LanguageMngTs.getSentence(188)  //You must bind your mobile.
                cc.gg.toastMgr.showDialog(title, positiveStr, passiveStr, content,
                    function () {
                        cc.gg.toastMgr.removeDialog()
                        // cc.find("Canvas/UIManager").emit("ui_show_kyc")
                        cc.loader.loadRes("windows/bindMobile", cc.Prefab, function (err, prefab) {
                            let newBindWin = cc.instantiate(prefab)
                            newBindWin.getComponent("BindMobile")
                            cc.find("Canvas").addChild(newBindWin)
                        }.bind(this))
                    }.bind(this),
                    function () {
                        cc.gg.toastMgr.removeDialog()
                    }
                )
            } else {
                cc.loader.loadRes("windows/Transaction", cc.Prefab, function (err, prefab) {
                    let newNode = cc.instantiate(prefab)
                    cc.find("Canvas/Windows").addChild(newNode)
                    newNode.getComponent("TransactionTs").getInvoiceList(0)
                    cc.loader.releaseRes("windows/Transaction")
                }.bind(this))
            }
        }

    },

    onHistoryButtonClicked: function () {
        cc.loader.loadRes("windows/WithdrawHistory", cc.Prefab, function (err, prefab) {
            let newNode = cc.instantiate(prefab)
            cc.find("Canvas/Windows").addChild(newNode)
            newNode.getComponent("WithdrawHistoryTs").getOtherHistory(1)
            cc.loader.releaseRes("windows/WithdrawHistory")
        }.bind(this))
    },

    onSeeNoticeButtonClicked: function (event) {
        let info = cc.gg.dataMgr.getPlayerInfo()
        this.labelContentTitle.string = cc.gg.gameDataUtil.getStringOfRupee(info.deposit + info.winnings)
        this.labelAddContentTitle.string = cc.gg.gameDataUtil.getStringOfRupee(info.deposit + info.winnings)
        cc.find("buy", this.node.getChildByName("deposit")).active = false
        cc.find("withdraw", this.node.getChildByName("withDraw")).active = true
    },

    async withDrawItem(itemId) {
        let kv = {
            keys: ["WithdrawItem"],
            values: [itemId.toString()]
        }
        let jsonStr = JSON.stringify(kv)
        cc.log("JS:TD发送的JSON STRING:", jsonStr)
        cc.gg.tdUtil.doSendTdEvent("click_withdraw_item", "Wallet", jsonStr)

        // this._tellWhereToTrans(itemId)
        // return
        let canWithdraw = await this._canWithDraw(itemId)
        if (!canWithdraw) {
            return
        }

        let mobile = cc.gg.dataMgr.getPlayerInfo().mobile_no
        if (!mobile && !TestConfig.ignoreMobile) {
            let title = LanguageMngTs.getSentence(3)
            let positiveStr = LanguageMngTs.getSentence(57)   //Yes
            let passiveStr = LanguageMngTs.getSentence(179)   //Cancel
            let content = LanguageMngTs.getSentence(188)  //You must bind your mobile.
            cc.gg.toastMgr.showDialog(title, positiveStr, passiveStr, content,
                function () {
                    cc.gg.toastMgr.removeDialog()
                    // cc.find("Canvas/UIManager").emit("ui_show_kyc")
                    cc.loader.loadRes("windows/bindMobile", cc.Prefab, function (err, prefab) {
                        let newBindWin = cc.instantiate(prefab)
                        newBindWin.getComponent("BindMobile")
                        cc.find("Canvas").addChild(newBindWin)
                    }.bind(this))
                }.bind(this),
                function () {
                    cc.gg.toastMgr.removeDialog()
                }
            )
        } else {
            this._tellWhereToTrans(itemId)
        }
    },

    _tellWhereToTrans: function (itemId) {
        let wdObj = cc.gg.gameDataUtil.getWithDrawObj(itemId)
        // this._showWithdrawDialog(wdObj.coin,LanguageMng.getSentence(166))
        // return
        let self = this
        let doWithdraw = function () {
            cc.log("user try to withdraw...")
            cc.gg.toastMgr.showNetWaitting(async function () {
                let rupee = wdObj.coin / 100
                cc.log("用户尝试提现：", rupee, "卢比")
                cc.gg.httpUtil.httpGet('/order/withdraw/2/process', { amount: rupee }, function (result) {
                    cc.gg.toastMgr.removeNetWaitting()
                    if (!result.succ) {
                        cc.gg.toastMgr.showToast(result.msg)
                        return
                    } else {
                        let status = parseInt(result.data.status)
                        if (status === 0) {
                            self._showWithdrawDialog(wdObj.coin, LanguageMngTs.getSentence(166))
                        } else if (status === 1) {
                            self._showWithdrawDialog(wdObj.coin, LanguageMngTs.getSentence(166))
                        } else if (status === 2) {
                            self._showWithdrawDialogFailed(LanguageMngTs.getSentence(167))
                        } else {
                            cc.gg.toastMgr.showToast(status)
                            cc.error("未知的status", status)
                        }
                    }
                })
                // cc.gg.httpUtil.httpPost('/order/withdraw/process',{packId:itemId},function (result) {
                //     cc.gg.toastMgr.removeNetWaitting()
                //     if(!result.succ){
                //         cc.gg.toastMgr.showToast(result.msg)
                //         return
                //     }else {
                //         let status = parseInt(result.data.status)
                //         if(status === 0){
                //             self._showWithdrawDialog(wdObj.coin,LanguageMngTs.getSentence(166))
                //         }else if(status === 1){
                //             self._showWithdrawDialog(wdObj.coin,LanguageMngTs.getSentence(166))
                //         }else if(status === 2){
                //             self._showWithdrawDialogFailed(LanguageMngTs.getSentence(167))
                //         }else {
                //             cc.gg.toastMgr.showToast(status)
                //             cc.error("未知的status",status)
                //         }
                //     }
                // })
            })
        }


        let title = LanguageMngTs.getSentence(3)
        let positiveStr = LanguageMngTs.getSentence(57)   //Yes
        let passiveStr = LanguageMngTs.getSentence(179)   //Cancel
        let content = LanguageMngTs.getSentence(187)  //your withdraw amount will be trans...
        cc.gg.toastMgr.showDialog(title, positiveStr, passiveStr, content,
            function () {
                cc.gg.toastMgr.removeDialog()
                doWithdraw()
            },
            function () {
                cc.gg.toastMgr.removeDialog()
            }
        )
    },

    _showWithdrawDialog: function (coin, sentence) {
        let cb = function () {
            cc.find("Canvas").emit("refresh_player_info")
            this.closeWallet()
        }.bind(this)

        cc.gg.toastMgr.showWithdrawSuccess(coin, sentence, cb)
    },

    _showWithdrawDialogFailed: function (sentence) {
        let title = LanguageMngTs.getSentence(3)
        let btnStr = LanguageMngTs.getSentence(57)
        let contentStr = sentence
        let cb = function () {
            cc.find("Canvas").emit("refresh_player_info")
            cc.gg.toastMgr.removeDialog()
            // this.closeWallet()
        }.bind(this)
        cc.gg.toastMgr.showDialogOneButton(title, btnStr, contentStr, cb)
    },

    async _canWithDraw(itemId) {
        let wdObj = cc.gg.gameDataUtil.getWithDrawObj(itemId)
        if (wdObj.coin > cc.gg.dataMgr.getPlayerInfo().winnings && !cc.gg.gameDataUtil.openKYCWithoutMoney) {
            cc.gg.toastMgr.showToast(LanguageMngTs.getSentence(165))
            return false
        }

        await cc.gg.toastMgr.showNetWaitting()
        let result = await cc.gg.httpUtil.httpGet('/user/doc/kyc/get', null)
        cc.gg.toastMgr.removeNetWaitting()
        if (!result.succ) {
            cc.error(result.msg)
            return
        } else {
            cc.gg.dataMgr.kycData = result.data
        }

        if (cc.gg.dataMgr.kycData.status === KYCEnum.NONE) {
            let a = LanguageMngTs.getSentence(3)    //Notice
            let b = LanguageMngTs.getSentence(239)   //Go to KYC
            let c = LanguageMngTs.getSentence(179)  //Cancel
            let d = LanguageMngTs.getSentence(231)  //You must verify kyc first.
            cc.gg.toastMgr.showDialog(a, b, c, d,
                function () {
                    // cc.gg.baseUtil.setLandscape(2)
                    // cc.director.loadScene('KYC')
                    cc.loader.loadRes("windows/KYC", cc.Prefab, function (err, prefab) {
                        let newNode = cc.instantiate(prefab)
                        cc.find("Canvas/Windows").addChild(newNode)
                        cc.loader.releaseRes("windows/KYC")
                    }.bind(this))
                }.bind(this),
                function () {
                    cc.gg.toastMgr.removeDialog()
                }.bind(this)
            )
            return false
        }

        if (cc.gg.dataMgr.kycData.status === KYCEnum.VERIFICATING) {
            cc.gg.toastMgr.showToast("Please wait for KYC verification.", 2)
            return false
        }

        if (cc.gg.dataMgr.kycData.status !== KYCEnum.PASSED) {
            cc.gg.toastMgr.showToast("Your KYC verification is failed,please check it and submit again.", 2)
            return false
        }

        return true
    },

    buyItem: function (itemId) {
        let itemObj = this.getBuyItem(itemId)
        let coinCount = itemObj.coin.toString()
        let kv = {
            keys: ["BuyItem", "BuyCount"],
            values: [itemId.toString(), coinCount]
        }
        let jsonStr = JSON.stringify(kv)
        cc.log("JS:TD发送的JSON STRING:", jsonStr)
        GameDataUtilTs.sendFbPurchase(coinCount)

        cc.gg.tdUtil.doSendTdEvent("click_buy_item", "Wallet", jsonStr)

        if (coinCount < 1 || coinCount > 5000000) {
            cc.gg.toastMgr.showToast("The amount is not correct,please contact customer service.", 1.5)
            return
        }

        cc.gg.toastMgr.showNetWaitting(async function () {
            let rupee = coinCount / 100
            cc.log("用户尝试购买：", rupee, "卢比")
            let result = await cc.gg.httpUtil.httpGet("/order/recharge/web/link/get", { amount: rupee })
            cc.gg.toastMgr.removeNetWaitting()
            if (!result.succ) {
                cc.gg.toastMgr.showToast(result.msg)
                return
            } else {
                let link = result.data.link
                cc.gg.baseUtil.setLandscape(2)
                cc.director.loadScene("WebScene", function () {
                    cc.find("Canvas").getComponent("WebScene").webNode.getComponent(cc.WebView).url = link
                })
            }
            // cc.gg.httpUtil.httpGet('/order/payment/web/link/get',{uid:cc.gg.dataMgr.getPlayerId(),packId:itemId},function (result) {
            //     cc.gg.toastMgr.removeNetWaitting()
            //     if(!result.succ){
            //         cc.gg.toastMgr.showToast(result.msg)
            //         return
            //     }else {
            //         let link = result.data.link
            //         cc.gg.baseUtil.setLandscape(2)
            //         cc.director.loadScene("WebScene",function () {
            //             cc.find("Canvas").getComponent("WebScene").webNode.getComponent(cc.WebView).url = link
            //         })
            //     }
            // }.bind(this))
        }.bind(this))
    }
});
